# coding=utf-8
from django.test import TestCase
from django.contrib.auth.models import User
from rating.calculators import ChildCount
from models import TestItem
from rating import register

class CalculatorTest(TestCase):
    def setUp(self):
        self.testuser = User.objects.create_user('test_u', 'test@test.com', '123')
        for i in range(10):
            ti = TestItem(user=self.testuser, rating=1)
            ti.save()
            del ti
    
    def testChildCount(self):
        countcalc = ChildCount(relation_name='testitem')
        self.assertEqual(countcalc.calc_self(self.testuser), 10)        
        
    def testOverload(self):
        countcalc = ChildCount(relation_name='testitem')*2
        self.assertEqual(countcalc.action_list, [['*', 2],])
        self.assertEqual(countcalc.calculate(self.testuser), 20)
        
        countcalc = ChildCount(relation_name='testitem')*0.1 * (ChildCount(relation_name='testitem')*2)
        self.assertEqual(countcalc.calculate(self.testuser), 20)
        
    def testRegister(self):
        register(User, ChildCount(relation_name='testitem')*2)
        self.assertEqual(self.testuser.rating, 20)